import Vue from 'vue'

/**
 *公共filter
 */
Vue.filter('setDistance', function (value) {
  value = value + ''
  if (value > 1) {
    // if(value.indexOf('.')>=0){
    //     if(value.toString().trim().substring(2, value.indexOf('.')+2)>0){
    var tmp = value.trim().substring(0, value.indexOf('.') + 2);
    tmp = tmp * 1000 + 10;
    return (tmp / 1000).toFixed(2) + 'km';
    // }else{
    //     return value.toString().trim().substring(0, value.indexOf('.')+2) + 'km';
    // }
    // }else{
    //     return value.toString().trim()
    // }
  }
  value = parseInt(value * 1000);
  if (value <= 100) {
    return ' < 100m';
  } else {
    return value + 'm';
  }
});
Vue.filter('endTime', function (vaule) {
  //console.info(new Date(vaule/1000));
  var time = new Date(vaule).format('Y-MM-dd hh:mm');
  return time;
});
Vue.filter('endTime2', function (vaule) {
  if (!vaule) {
    return;
  }
  if (vaule.toString().length == 8) {
    return vaule.substring(0, 4) + '.' + vaule.substring(4, 6) + '.' + vaule.substring(6, 8);
  }
  var time = new Date(vaule).format('Y.MM.dd');
  return time;
});
Vue.filter('endTimeDian', function (vaule) {
  var timeDian = new Date(vaule).format('Y.MM.dd');
  return timeDian;
});
Vue.filter('endTimeDianC', function (vaule) {
  var timeDian = new Date(vaule).format('Y年MM月dd日');
  return timeDian;
});
Vue.filter('endtimeStamp', function (vaule) {
  if (!vaule) {
    return;
  }
  var stringTime = vaule.substring(0, 4) + '/' + vaule.substring(4, 6) + '/' + vaule.substring(6, 8) + ' ' + vaule.substring(8, 10) + ':' + vaule.substring(10, 12) + ':' + vaule.substring(12, 14);
  var timeStamp = Date.parse(new Date(stringTime));
  var endtimeStampt = new Date(timeStamp).format('Y-MM-dd hh:mm');
  return endtimeStampt;
});
Vue.filter('endtimeStampt', function (vaule) {
  if (!vaule) {
    return;
  }
  var stringTime = vaule.substring(0, 4) + '/' + vaule.substring(4, 6) + '/' + vaule.substring(6, 8);
  var timeStamp = Date.parse(new Date(stringTime));
  var endtimeStampt = new Date(timeStamp).format('Y.MM.dd');
  return endtimeStampt;
});
Vue.filter('userSex', function (vaule) {
  if (vaule == 'man') {
    return '男'
  } else if (vaule == 'woman') {
    return '女'
  } else {
    return '保密'
  }
});
Vue.filter('income', function (vaule) {
  if (vaule == 'plus') {
    return '+'
  } else if (vaule == 'minus') {
    return '-'
  }
});
Vue.filter('paymentTp', function (vaule) {
  if (vaule == 'sale') {
    return '出售'
  } else if (vaule == 'buy') {
    return '购买'
  } else if (vaule == 'login') {
    return '登录'
  } else if (vaule == 'register') {
    return '注册'
  } else if (vaule == 'comment') {
    return '评论'
  } else if (vaule == 'giveup') {
    return '点赞'
  } else if (vaule == 'act') {
    return '活动'
  } else if (vaule == 'expand') {
    return '推广'
  } else if (vaule == 'reward') {
    return '打赏'
  } else if (vaule == 'pay') {
    return '买单'
  } else if (vaule == 'actSend' || vaule == 'actDuct') {
    return '游戏'
  } else if (vaule == 'propBuy') {
    return '道具'
  } else if (vaule == 'invite') {
    return '邀请'
  } else if (vaule == 'prep') {
    return '充值'
  } else if (vaule == 'cash') {
    return '转账'
  } else if (vaule == 'transfer') {
    return '提现'
  } else if (vaule == 'buy') {
    return '购券'
  } else if (vaule == 'deposit') {
    return '押金'
  } else if (vaule == 'market') {
    return '奖励金'
  } else if (vaule == 'back') {
    return '退券'
  } else if (vaule == 'confirm') {
    return '充值确认'
  } else if (vaule == 'SCAN') {
    return '扫码'
  } else if (vaule == 'SHARE' || vaule == 'LASTSHARE') {
    return '分享'
  } else if (vaule == 'USE') {
    return '使用'
  }
});
Vue.filter('paymentTp2', function (vaule) {
  if (vaule == 'sale') {
    return '出售'
  } else if (vaule == 'buy') {
    return '购买'
  } else if (vaule == 'login') {
    return '登录'
  } else if (vaule == 'register') {
    return '注册'
  } else if (vaule == 'comment') {
    return '评论'
  } else if (vaule == 'giveup') {
    return '点赞'
  } else if (vaule == 'act') {
    return '活动'
  } else if (vaule == 'expand') {
    return '推广'
  } else if (vaule == 'reward') {
    return '打赏'
  } else if (vaule == 'pay') {
    return '买单'
  } else if (vaule == 'actSend' || vaule == 'actDuct') {
    return '游戏'
  } else if (vaule == 'propBuy') {
    return '道具'
  } else if (vaule == 'invite') {
    return '邀请'
  } else if (vaule == 'prep') {
    return '充值'
  } else if (vaule == 'cash') {
    return '转账'
  } else if (vaule == 'transfer') {
    return '提现至银行卡'
  } else if (vaule == 'buy') {
    return '购买现金券'
  } else if (vaule == 'deposit') {
    return '押金'
  } else if (vaule == 'market') {
    return '营销'
  } else if (vaule == 'SCAN') {
    return '扫码奖励金'
  } else if (vaule == 'SHARE') {
    return '分享奖励金'
  } else if (vaule == 'LASTSHARE') {
    return '最后分享奖励金'
  } else if (vaule == 'USE') {
    return '使用优惠券奖励金'
  } else if (vaule == 'back') {
    return '退券'
  } else if (vaule == 'confirm') {
    return '充值确认'
  }
});
Vue.filter('couponTp', function (vaule) {
  if (vaule == 'cash') {
    return '代金券'
  } else if (vaule == 'rebate') {
    return '折扣券'
  } else if (vaule == 'voucher') {
    return '抵用券'
  } else if (vaule == 'present') {
    return '礼品券'
  } else if (vaule == 'ticket') {
    return '票务'
  }
});
Vue.filter('couponUnit', function (vaule) {
  if (vaule == 'cash' || vaule == 'voucher') {
    return '元'
  } else if (vaule == 'rebate') {
    return '折'
  }
});
Vue.filter('couponDollar', function (vaule) {
  if (vaule == 'cash' || vaule == 'voucher') {
    return '¥'
  } else if (vaule == 'rebate') {
    return ''
  }
});
Vue.filter('hisType', function (vaule) {
  var str = vaule.slice(0, 1);
  console.info(typeof str);
  if (str == '1') {
    return 'coupon'
  } else {
    return 'mer'
  }
});
Vue.filter('strDate', function (vaule, type) {
  if (!vaule) {
    return
  }
  if (vaule.length == 8) {
    vaule = vaule + '000000'
  }
  var stringTime = vaule.substring(0, 4) + '/' + vaule.substring(4, 6) + '/' + vaule.substring(6, 8) + ' '
    + vaule.substring(8, 10) + ':' + vaule.substring(10, 12) + ':' + vaule.substring(12, 14);
  if (type == 1) {
    return new Date(stringTime).format("Y-MM-dd");
  } else if (type == 2) {
    return new Date(stringTime).format("Y年MM月dd日");
  } else if (type == 3) {
    return new Date(stringTime).format("Y.MM.dd");
  } else {
    return new Date(stringTime).format("Y-MM-dd hh:mm:ss");
  }

});
//截取小数后一位
Vue.filter('interceptStr', function (vaule) {
  if (!vaule) {
    return
  }
  // var string = JSON.stringify(vaule).substring(0,JSON.stringify(vaule).indexOf(".")+2);
  var string = parseFloat(vaule).toFixed(1);
  return string;
});
Vue.filter('formatMoney', function (vaule) {
  if (!vaule) {
    return 0
  }
  var string = parseFloat(vaule).toFixed(2);
  return string;
});
Vue.filter("denomination", function (value, param) {
  if (param == 'rebate') {
    var _str = parseInt(value) / 10 + '折'
    return _str;
  } else {
    var _str = value + '元'
    return _str
  }
});
Vue.filter("hideName", function (value) {
  var str = value.substring(0, 1) + value.replace(/(.)/g, "*");
  return str

});
Vue.filter("orderState", function (value) {
  if (!value) {
    return;
  }
  if (value.orderState == 'normal') {
    return '立即买单'
  } else if (value.orderState == 'payed') {
    return '支付完成'
  } else {
    return '立即买单'
  }
});
// 数字解析成星期
Vue.filter('businessDay', function (value) {
  if (!value) {
    return
  }
  var arrange = function (arr) {

    var result = [],
      temp = [],
      strs = [];
    strs = arr.split(",");
    for (var i = strs.length - 1; i >= 0; i--) {
      if (!strs[i]) {
        strs.splice(i, 1)
      }
    }
    // if (strs[strs.length - 1] == "") {
    //     strs.splice(strs.length - 1, 1)
    // }
    strs.sort(function (source, dest) {
      return source - dest;
    }).concat(Infinity).reduce(function (source, dest) {

      temp.push(source);

      if (dest - source > 1) {
        result.push(temp);
        temp = [];
      }
      return dest;
    });
    return result;

  };

  function weekT(value) {
    var weekArry = [];
    var weekStr = '';
    var string = "周";
    for (var i = 0; i < value.length; i++) {
      switch (Number(value[i])) {

        case 1 :
          string += "一";
          break;
        case 2 :
          string += "二";
          break;
        case 3 :
          string += "三";
          break;
        case 4 :
          string += "四";
          break;
        case 5 :
          string += "五";
          break;
        case 6 :
          string += "六";
          break;
        case 7 :
          string += "日";
          break;
      }
      ;
      weekArry.push(string);
      string = "周";
      if (value.length > 1) {
        weekStr = weekArry[0] + '至' + weekArry[weekArry.length - 1]
      } else {
        weekStr = weekArry[0];
      }
    }
    return weekStr;
  }

  var weekValue = '';
  if (value.length == 1) {
    var string1 = "周";
    switch (Number(arrange(value))) {

      case 1 :
        string1 += "一";
        break;
      case 2 :
        string1 += "二";
        break;
      case 3 :
        string1 += "三";
        break;
      case 4 :
        string1 += "四";
        break;
      case 5 :
        string1 += "五";
        break;
      case 6 :
        string1 += "六";
        break;
      case 7 :
        string1 += "日";
        break;
    }
    ;
    weekValue = string1;
    string1 = "周";
  } else {
    for (var i = 0; i < arrange(value).length; i++) {
      weekValue = weekValue + ' ' + weekT(arrange(value)[i]);
    }
  }

  return weekValue
});

Vue.filter("arryThree", function (value) {
  console.log(arryThree);
  if (!value) {
    return;
  }
});
Vue.filter("strTrim", function (value) {
  // console.log(value);
  if (!value) {
    return;
  }
  ;
  return value.trim()
});
// base64解密
Vue.filter("comdeCode", function (value) {
  if (!value) {
    return value;
  }
  var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
  if (reg.test(value)) {
    return value;
  }
  try {
    if (!Base64.decode(value)) {
      return value;
    }
  } catch (e) {
    return value;
  }

  var str = Base64.decode(value);
  return str
});
Vue.filter("sliceString", function (value, num) {
  if (value.length <= num) {
    return value
  } else {
    return value.slice(0, parseInt(num)) + '...'
  }

});